package com.coincalf.wallet.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.coincalf.framework.dto.CoinRechargeDTO;
import com.coincalf.framework.enums.RechargeStatus;
import com.coincalf.framework.http.Response;
import com.coincalf.wallet.dto.CoinRechargeDTOMapper;
import com.coincalf.wallet.entity.CoinRecharge;
import com.coincalf.wallet.mapper.CoinRechargeMapper;
import com.coincalf.wallet.service.CoinRechargeService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 用户充值,当前用户充值成功之后添加数据到这个表,充值一般无手续费.当status为1的时候表示充值成功 服务实现类
 * </p>
 *
 * @author crow
 * @since 2018-05-16
 */
@Service
public class CoinRechargeServiceImpl extends ServiceImpl<CoinRechargeMapper, CoinRecharge> implements CoinRechargeService {

    /**
     * 获取充值记录集合
     *
     * @param page         分页参数
     * @param agencyUserId 代理人用户ID
     * @param userName     用户名
     * @param orderId      订单号
     * @param status       状态
     * @param coinId       币种ID
     * @param parentId     上级ID
     * @param createTime   创建时间
     * @return
     */
    @Override
    public Page<CoinRecharge> selectRecharges(Page<CoinRecharge> page,
                                              long agencyUserId,
                                              String userName,
                                              long orderId,
                                              Integer status,
                                              long coinId,
                                              long parentId,
                                              String createTime) {
        List<CoinRecharge> list = baseMapper.selectRecharges(page, agencyUserId, userName, orderId, status, coinId, parentId, createTime);
        return page.setRecords(list);
    }

    /**
     * 获取充值记录统计
     *
     * @param agencyUserId 代理用户ID
     * @return
     */
    @Override
    public Response selectRechargesTotal(long agencyUserId) {
        return Response.ok(baseMapper.selectRechargesTotal(agencyUserId));
    }

    /**
     * 查询待入账的充值记录
     *
     * @return
     */
    @Override
    public List<CoinRechargeDTO> queryPendingRecord() {
        EntityWrapper<CoinRecharge> wrapper = new EntityWrapper<>();
        wrapper.eq("status", RechargeStatus.PENDING.getCode())
                .orderBy("created", true);
        List<CoinRecharge> coinRecharges = baseMapper.selectList(wrapper);
        return CoinRechargeDTOMapper.INSTANCE.from(coinRecharges);
    }

    /**
     * 修改充值记录状态
     *
     * @param id     充值记录主键
     * @param status 状态
     * @return
     */
    @Override
    @Transactional
    public boolean modifyRechargeStatus(Long id, Integer status, Integer collected) {
        return baseMapper.modifyRechargeStatus(id, status, collected) == 1;
    }

    @Override
    public CoinRecharge selectOneByCoinIdAndTxIdAndToAddress(Long coinId, String txId, String toAddress) {
        EntityWrapper<CoinRecharge> wrapper = new EntityWrapper<>();
        wrapper.eq("coin_id", coinId).eq("txid", txId).eq("address", toAddress);
        return super.selectOne(wrapper);
    }

    @Override
    public List<CoinRecharge> findAllByAndCoinNameAndTxStatusAndCollected(String coinName, Integer txStatus, Integer collected) {
        EntityWrapper<CoinRecharge> wrapper = new EntityWrapper<>();
        wrapper.eq("coin_name", coinName).eq("status", txStatus).eq("collected", collected);
        return super.selectList(wrapper);
    }

    @Override
    public List<CoinRecharge> findAllByCoinNameAndTxStatusAndCollectedAndToAddress(String coinName, Integer txStatus, Integer collected, String toAddress) {
        EntityWrapper<CoinRecharge> wrapper = new EntityWrapper<>();
        wrapper.eq("coin_name", coinName).eq("status", txStatus).eq("collected", collected).eq("address", toAddress);
        return super.selectList(wrapper);
    }

    @Override
    public int updateTxStatus(Long id, Integer txStatus) {
        return baseMapper.updateTxStatus(id, txStatus);
    }

}
